<?php
if (!(empty($_POST)))  //不为空
{
    $username=isset($_POST["username"]) ? trim($_POST["username"])  : "";
    $password=isset($_POST["password"]) ? trim($_POST["password"])  : "";
    $re=isset($_POST["re"]) ? trim($_POST["re"])  : false;
    
    //判断用户登录是否成功，用户名，数据库里没有没，密码是否正确    
    require_once 'db_conn.php';
    $sql=" select * from t_user where username='$username'";
    $rs=$db->query($sql);
    if ($rs)  //有这个名字的用户
    {
        $row=$rs->fetch_assoc();
        if ($row['password']==$password) //密码也正确
        {
            //把登录成功的用户信息存在session
            session_start();
            $_SESSION["userinfo"]=array(
                "id"=>$row['id'],
                "username"=>$row['username']
            );
            header("Location:main.php");
            die;
        }
        
    }
           
        echo "<script language='javascript' type='text/javascript'>";
        echo "alert('登录失败,用户名和密码不正确！');";
        echo "window.location.href='index.php'";
        echo "</script>";    
    
}



